﻿using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
///事件管理
/// </summary>
public static class EventManager
{
    /// <summary>
    /// 存储注册好的事件
    /// </summary>
    private static readonly Dictionary<EventEnum, EventMgr> EventListerDict = new Dictionary<EventEnum, EventMgr>();

    /// <summary>
    /// 存储注册好的字符串事件
    /// </summary>
    private static readonly Dictionary<string, EventMgr> EventListerDict_Str = new Dictionary<string, EventMgr>();


    /// <summary>
    /// 是否暂停所有的事件
    /// </summary>
    public static bool IsPause = false;
    static EventManager()
    {
        Assets.Script.Tools.EventManagerConfigs.UIEventsConfigs.EventsConfigs();
    }

    /// <summary>
    /// 方法是否注册过
    /// </summary>
    /// <param name="eventMgr"></param>
    public static bool IsRegisterFunc(EventMgr eventMgr)
    {
        if (EventListerDict.ContainsValue(eventMgr) || EventListerDict_Str.ContainsValue(eventMgr))
        {
            Debug.Log("事件已注册！");
            return true;
        }
        Debug.Log("事件未注册！");
        return false;
    }

    /// <summary>
    /// 取消所有事件绑定
    /// </summary>
    public static void ClearAll()
    {
        if (EventListerDict_Str != null)
        {
            EventListerDict_Str.Clear();
            Debug.Log("清空注册事件！");
        }
        if (EventListerDict != null)
        {
            EventListerDict.Clear();
            Debug.Log("清空注册事件！");
        }
    }




    #region 字符串事件管理
    /// <summary>
    /// 增加事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="eventMgr"></param>
    public static void Add(string key, EventMgr eventMgr)
    {
        if (EventListerDict_Str.ContainsKey(key))
        {
            EventListerDict_Str[key] += eventMgr;
        }
        else
        {
            EventListerDict_Str.Add(key, eventMgr);
        }
    }

    /// <summary>
    /// 取消事件绑定
    /// </summary>
    /// <param name="key"></param>
    public static void Reomve(string key, EventMgr eventMgr)
    {
        if (EventListerDict_Str.ContainsKey(key))
        {
            EventListerDict_Str[key] -= eventMgr;
        }
    }



    /// <summary>
    /// 调用事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="param"></param>
    public static void Invoke(string key, params object[] param)
    {
        if (!IsPause)
        {
            if (EventListerDict_Str.ContainsKey(key))
            {
                if (EventListerDict_Str[key] == null)
                {
                    EventListerDict_Str.Remove(key);
                }
                else
                {
                    EventListerDict_Str[key].Invoke(param);
                }
            }
        }
        else
        {
            Debug.LogError("所有事件已暂停！");
        }

    }

    /// <summary>
    /// 取消所有字符串事件绑定
    /// </summary>
    public static void ClearAll_Str()
    {
        if (EventListerDict_Str != null)
        {
            EventListerDict_Str.Clear();
            Debug.Log("清空注册事件！");
        }
    }
    #endregion

    #region 枚举事件管理

    /// <summary>
    /// 取消所有枚举事件绑定
    /// </summary>
    public static void ClearAll_Enum()
    {
        if (EventListerDict != null)
        {
            EventListerDict.Clear();
            Debug.Log("清空注册事件！");
        }
    }

    /// <summary>
    /// 增加事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="eventMgr"></param>
    public static void Add(EventEnum key, EventMgr eventMgr)
    {
        if (EventListerDict.ContainsKey(key))
        {
            EventListerDict[key] += eventMgr;
        }
        else
        {
            EventListerDict.Add(key, eventMgr);
        }
    }

    /// <summary>
    /// 取消事件绑定
    /// </summary>
    /// <param name="key"></param>
    public static void Reomve(EventEnum key, EventMgr eventMgr)
    {
        if (EventListerDict.ContainsKey(key))
        {
            EventListerDict[key] -= eventMgr;
        }
    }


    /// <summary>
    /// 调用事件
    /// </summary>
    /// <param name="key"></param>
    /// <param name="param"></param>
    public static void Invoke(EventEnum key, params object[] param)
    {
        if (!IsPause)
        {
            if (EventListerDict.ContainsKey(key))
            {
                EventListerDict[key].Invoke(param);
            }
        }
        else
        {
            Debug.LogError("所有事件已暂停！");
        }

    }
    #endregion










}

